package com.syh.jn.scm.service.impl.supplier.base.upstream;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.base.upstream.Upstream;
import com.syh.jn.scm.domain.supplier.entity.base.upstream.UpstreamAccount;
import com.syh.jn.scm.domain.supplier.event.UpstreamEvent;
import com.syh.jn.scm.domain.supplier.repository.base.upstream.UpstreamAccountRepository;
import com.syh.jn.scm.domain.supplier.repository.base.upstream.UpstreamRepository;
import com.syh.jn.scm.dto.supplier.base.upstream.UpstreamAccountMto;
import com.syh.jn.scm.dto.supplier.base.upstream.UpstreamAccountSto;
import com.syh.jn.scm.dto.supplier.base.upstream.supplier.UpstreamMto;
import com.syh.jn.scm.dto.supplier.base.upstream.supplier.UpstreamSto;
import com.syh.jn.scm.dto.supplier.base.upstream.supplier.UpstreamVo;
import com.syh.jn.scm.dto.supplier.base.upstream.supplierAddress.UpstreamAddressVo;
import com.syh.jn.scm.service.api.supplier.base.upstream.UpstreamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class UpstreamServiceImpl extends AbstractServiceImpl<UpstreamSto, UpstreamMto, UpstreamVo, Upstream, Long> implements UpstreamService {

  @Autowired
  private UpstreamRepository upstreamSupplierRepositoryImpl;
  @Autowired
  private UpstreamAccountRepository upAccountRepositoryImpl;

  @Override
  protected AggregateRepository<Upstream, Long> getAggregateRepository() {
    return upstreamSupplierRepositoryImpl;
  }

  /**
   * 保存上游和地址项
   *
   * @param sto
   * @return
   */
  @Override
  public Long save(UpstreamSto sto) {
    Upstream upstream = Upstream.createBusiness(sto);

    Long id = upstreamSupplierRepositoryImpl.add(upstream);

    Upstream upstreamMq = upstreamSupplierRepositoryImpl.getById(id);
    //发送事件
    this.publicEvent(UpstreamEvent.buildUpstreamCreatedEvent(new Object[]{upstreamMq}));
    return id;
  }

  /**
   * 修改上游和地址项
   *
   * @param mto
   */
  @Override
  public void update(UpstreamMto mto) {
    Upstream upstream = upstreamSupplierRepositoryImpl.getById(mto.getId());
    upstream.modify(mto);
    upstreamSupplierRepositoryImpl.update(upstream);
  }

  /**
   * 删除上游和地址项
   *
   * @param id
   */
  @Override
  public void delete(Long id) {
    Upstream upstream = upstreamSupplierRepositoryImpl.getById(id);
    upstream.destroy();
    upstreamSupplierRepositoryImpl.remove(upstream);
    this.publicEvent(UpstreamEvent.buildUpstreamDeletedEvent(new Object[]{upstream}));
  }

  /**
   * 添加账号
   *
   * @param sto
   * @return
   */
  @Override
  public Boolean addAccount(UpstreamAccountSto sto) {
    UpstreamAccount account = new UpstreamAccount(sto);
    return upAccountRepositoryImpl.addAccount(account);
  }
  /**
   * 修改账号
   *
   * @param mto
   * @return
   */
  @Override
  public Boolean setAccount(UpstreamAccountMto mto) {
      UpstreamAccount upAccount = upAccountRepositoryImpl.getById(mto.getId());
      upAccount.modify(mto);
      ExtBeanUtils.copyProperties(mto,upAccount,false);
      upAccountRepositoryImpl.update(upAccount);
      return true;
  }

  /**
   * 启用
   *
   * @param id
   */
  @Override
  public void enabled(Long id) {
    Upstream upstream = upstreamSupplierRepositoryImpl.getById(id);
    upstream.enabled();
    upstreamSupplierRepositoryImpl.updateStatus(upstream);
  }

  /**
   * 停用上游和地址项
   *
   * @param id
   */
  @Override
  public void disabled(Long id) {
    Upstream upstream = upstreamSupplierRepositoryImpl.getById(id);
    upstream.disabled();
    upstreamSupplierRepositoryImpl.updateStatus(upstream);
  }

  @Override
  public List<UpstreamAddressVo> getAddressListById(Long id) {
    return upstreamSupplierRepositoryImpl.getAddressListById(id);
  }

  @Override
  public List<Long> batchSave(List<UpstreamSto> upstreamStos) {
    List<Long> ids = new ArrayList<>();
    upstreamStos.stream().forEach(sto -> {
      Long id = this.save(sto);
      ids.add(id);
    });
    return ids;
  }
}
